package com.ruoyi.hotel.controller;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.hotel.domain.HmBook;
import com.ruoyi.hotel.domain.HmCheckin;
import com.ruoyi.hotel.domain.HmRoom;
import com.ruoyi.hotel.domain.HmRoomType;
import com.ruoyi.hotel.domain.bo.HmBillBo;
import com.ruoyi.hotel.domain.bo.HmBookBo;
import com.ruoyi.hotel.domain.bo.HmCheckinBo;
import com.ruoyi.hotel.domain.bo.HmRoomBo;
import com.ruoyi.hotel.domain.vo.HmBookVo;
import com.ruoyi.hotel.domain.vo.HmCheckinVo;
import com.ruoyi.hotel.mapper.HmBookMapper;
import com.ruoyi.hotel.mapper.HmRoomMapper;
import com.ruoyi.hotel.mapper.HmRoomTypeMapper;
import com.ruoyi.hotel.service.IHmBookService;
import com.ruoyi.hotel.service.IHmRoomService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.service.IHmCheckinService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入住管理操作
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/checkin")
public class HmCheckinController extends BaseController {

    private final IHmCheckinService iHmCheckinService;
    private final IHmBookService iHmBookService;

    /**
     * 查询入住列表
     */
    @SaCheckPermission("hotel:checkin:list")
    @GetMapping("/list")
    public TableDataInfo<HmCheckinVo> list(HmCheckinBo bo, PageQuery pageQuery) {
        bo.setIsDone(true);
        return iHmCheckinService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取单个入住详细信息
     *
     * @param checkinId 主键
     */
    @SaCheckPermission("hotel:checkin:query")
    @GetMapping("/{checkinId}")
    public R<HmCheckinVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long checkinId) {
        return R.ok(iHmCheckinService.queryById(checkinId));
    }

    /**
     * 新增入住（不推荐）
     */
    @SaCheckPermission("hotel:checkin:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmCheckinBo bo) {
        return toAjax(iHmCheckinService.insertByBo(bo));
    }

    /**
     * 修改入住
     */
    @SaCheckPermission("hotel:checkin:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmCheckinBo bo) {
        return toAjax(iHmCheckinService.updateByBo(bo));
    }


    /**
     * 入住登记
     * @param book
     */
    @RequestMapping("/doCheckin")
    public R<Void> checkin(@RequestBody HmBookBo book){
        //从预约信息中获取，生产入住对象
        HmCheckinBo hmCheckin=new HmCheckinBo();
        hmCheckin.setBookId(book.getBookId());
        hmCheckin.setCheckinWorkerId(getUserId());
        hmCheckin.setCheckinTime(new Date());
        hmCheckin.setCheckoutTime(book.getCheckoutTime());
        hmCheckin.setCustomerId(book.getCustomerId());
        hmCheckin.setRoomId(book.getRoomId());
        hmCheckin.setIsDone(false);

        //更新预约，使其无效
        book.setIsExist("n");
        iHmBookService.updateByBo(book);

        return toAjax(iHmCheckinService.insertByBo(hmCheckin));
    }

    /**
     * 续住操作
     * @param checkin
     */
    @RequestMapping("/doExtension")
    public R<Void> doExtension(@RequestBody HmCheckinBo checkin){
        //续住一般为一天，如果有特殊情况用修改方法直接加一定天数
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(checkin.getCheckoutTime());
        calendar.add(Calendar.DAY_OF_MONTH,1);
        checkin.setCheckoutTime(calendar.getTime());

        return toAjax(iHmCheckinService.updateByBo(checkin));
    }

    /**
     * 取消预约（是在入住登记时失败了的操作，相当于把此次预约直接作废）
     * @param book
     */
    @RequestMapping("/cancelBook")
    public void cancelBook(@RequestBody HmBook book){

        iHmBookService.cancelBook(book);
        //直接作废
        book.setIsExist("q");
        iHmBookService.updateById(book);

    }



}
